<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%@include file="/common/common.jsp" %>
    <link rel="stylesheet" href="${basePath}/css/layerPage.css${VERSION}"/>
    <link rel="stylesheet" href="${basePath}/plugins/ztree/zTreeStyle/zTreeStyle.css${VERSION}" type="text/css">
</head>
<body>

<div class="content_wrap">
    <div>
        <input type="hidden" id="funRoleId" value="${funRoleId}">
        <input type="hidden" id="orgId" value="${orgId}">
        <ul id="tree" class="ztree"></ul>
        <button id="updateButton" type="button" class="btn btn-success btn-submit">提交</button>
    </div>
</div>

<script type="text/javascript" src="${basePath}/plugins/ztree/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${basePath}/plugins/ztree/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    $(document).ready(function () {
        var funRoleId = $("#funRoleId").val();
        if (funRoleId == '' || funRoleId == null) {
            return;
        }
        var zNodes = [];
        $.ajax({
            url: "${basePath}/funRole/anon/findUsersByfunRoleId",
            dataType: "json",
            data: {
                funRoleId: funRoleId,
                orgId:$("#orgId").val()
            },
            type: "post",
            async: false,
            success: function (data) {
                if (data.status == 0) {
                    zNodes = data.data;
                } else {
                    layer.alert(data.message, {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                }
            },
            error: function (result) {
                layer.alert("系统未知错误", {
                    icon: 0,
                    skin: 'layui-layer-lan',
                    closeBtn: 0
                });
            }
        });
        $.fn.zTree.init($("#tree"), setting, zNodes);
    });

    var $updateButton = $("#updateButton");
    $updateButton.click("click", function () {
        var zTree = $.fn.zTree.getZTreeObj("tree");
        var selectedNode = zTree.getCheckedNodes(true);
        var userIds = [];
        for (var i = 0; i < selectedNode.length; i++) {
            userIds.push(selectedNode[i].id);
        }
        $.ajax({
            url: "${basePath}/funRole/chooseUser",
            type: "post",
            data: "orgId=" + $("#orgId").val() + "&userIds=" + userIds+"&funRoleId="+$("#funRoleId").val(),
            dataType: "json",
            cache: false,
            beforeSend: function () {
                $updateButton.prop("disabled", true);
            },
            success: function (data) {
                if (data.status == '0') {
                    $updateButton.prop("disabled", false);
                    layer.confirm('操作成功！是否关闭窗口？', {icon: 1}, function (index) {
                        parent.$("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
                        parent.layer.close(parent.pageii);
                        return false
                    });
                } else {
                    $updateButton.prop("disabled", false);
                    layer.alert(data.message, {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                $updateButton.prop("disabled", false);
                layer.alert("系统未知错误", {
                    icon: 0,
                    skin: 'layui-layer-lan',
                    closeBtn: 0
                });
            }
        });
    });
</script>
</body>
</html>